package com.nameli.admin.controller;

import com.github.pagehelper.PageInfo;
import com.nameli.SIM.bean.dto.CheckMajorDTO;
import com.nameli.SIM.bean.vo.CheckMajorVO;
import com.nameli.SIM.util.response.JsonResult;
import com.nameli.SIM.util.EmptyUtil;
import com.nameli.SIM.util.ReturnStatus;
import com.nameli.admin.service.CheckMajorInfoService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author NAME-L
 * @Description 专业信息审核操作 控制层
 * @className CheckMajorInfoController
 * @date 2021-11-23 16:19:31
 */
@RestController
@CrossOrigin
@RequestMapping("/checkMajor")
public class CheckMajorInfoController {
    private static final Log log = LogFactory.getLog(CheckMajorInfoController.class);

    @Autowired
    private CheckMajorInfoService checkMajorInfoService;

    /**
     * 显示所有审核记录
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/index")
    public JsonResult queryAllCheckMajor(@Param("pageIndex") Integer pageIndex , @Param("pageSize") Integer pageSize){
        JsonResult jsonResult = null;
        log.info("enter into -----------> CheckMajorInfoController - queryAllCheckMajor");
        try {
            if (EmptyUtil.isNotEmpty(pageIndex) && EmptyUtil.isNotEmpty(pageSize)){
                log.info("pageIndex:" + pageIndex + "\t" + "pageSize:"+pageSize);
                log.info("check -----------> pageIndex & pageSize 不为空");
                PageInfo<CheckMajorVO> returnList = checkMajorInfoService.queryAllCheckMajor(pageIndex, pageSize);
                jsonResult = JsonResult.returnMethod(ReturnStatus.SUCCESS, "查询成功", returnList);;
            }else {
                log.info("check -----------> pageIndex & pageSize 为空");
                jsonResult = JsonResult.withoutData(ReturnStatus.DO_FAILED, "查询失败");
            }
        } catch (Exception e) {
            jsonResult = JsonResult.withoutData(ReturnStatus.ERROR, "系统异常");
            log.info("输出异常e:" + e);
        }
        return jsonResult;
    }

    @PutMapping("/check")
    public JsonResult updateCheckMajor(@RequestBody CheckMajorDTO checkMajorDTO){
        JsonResult jsonResult = null;
        Map<String, Object> returnMap = new HashMap<>();
        log.info("enter into -----------> CheckMajorInfoController - updateCheckMajor");
        Boolean flag = false;
        try {
            String type = checkMajorDTO.getCheckType();
            if ("1".equals(type)){
                log.info("审核通过操作----------->");
                flag = checkMajorInfoService.updateCheckMajor(checkMajorDTO);
            }else if ("2".equals(type)){
                log.info("审核驳回操作----------->");
                flag = checkMajorInfoService.updateRejectMajor(checkMajorDTO);
            }
            log.info("专业信息审核flag:" + flag);
            returnMap.put("flag", flag);
            if (flag){
                log.info("专业信息审核通过");
                jsonResult = JsonResult.returnMethod(ReturnStatus.CREATED, "审核成功",returnMap);
            }else{
                log.info("专业信息审核失败");
                jsonResult = JsonResult.returnMethod(ReturnStatus.DO_FAILED, "审核失败" ,returnMap);
            }
        } catch (Exception e) {
            jsonResult = JsonResult.withoutData(ReturnStatus.ERROR, "系统异常");
            log.error("捕获异常e:" + e);
            return jsonResult;
        }
        return jsonResult;
    }

}
